package com.kalvin.kvf.modules.sys.controller;

import cn.hutool.core.io.resource.ClassPathResource;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

@RestController
@RequestMapping(value = "sys/cards")
public class CardsController {

    @RequiresPermissions("sys:cards:index")
    @GetMapping("index")
    public ModelAndView index(){
        return new ModelAndView("cards/index");
    }

    @GetMapping("getJson")
    public String getJson() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("static/data/normal.json");

        File file = classPathResource.getFile();
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        StringBuilder content = new StringBuilder();
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            content.append(line);
        }
        return content.toString();
    }

    @GetMapping("selectType/{type}")
    public String getChangZhu(@PathVariable int type) throws IOException{
        ClassPathResource classPathResource = null;
        switch (type){
            case 0:
               classPathResource = new ClassPathResource("templates/cards/changzhu.html");
                break;
            case 1:
                classPathResource = new ClassPathResource("templates/cards/xianding.html");
        }

        File file = classPathResource.getFile();
        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
        StringBuilder content = new StringBuilder();
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            content.append(line);
        }
        return content.toString();
    }
}
